#include "MyTime.h"



void CurrentTime::timeInitial(NTPClient timeClient) {
  timeClient.update();

  time_t epochTime = timeClient.getEpochTime();
  Serial.print("Epoch Time:");
  Serial.println(epochTime);

  //将epochTime换算成年月日
  struct tm *ptm = localtime(&epochTime);

  // 年
  int year = ptm->tm_year + 1900;
  this->year = year;
  Serial.print("Year:");
  Serial.println(year);

  //月
  int month = ptm->tm_mon + 1;
  this->month = month;
  Serial.print("Month:");
  Serial.println(month);

  //日
  int day = ptm->tm_mday;
  this->day = day;
  Serial.print("Month day:");
  Serial.println(day);

  //时
  int hour = ptm->tm_hour;
  this->hour = hour;
  Serial.print("Hour:");
  Serial.println(hour);

  //分
  int minute = ptm->tm_min;
  this->minute = minute;
  Serial.print("Minute:");
  Serial.println(minute);

  //秒
  int second = ptm->tm_sec;
  this->second = second;
  Serial.print("Second:");
  Serial.println(second);

  //星期几
  int weekday = ptm->tm_wday;
  this->weekday = weekday;
  Serial.print("Week day:");
  Serial.println(weekday);
}

// 判断闰年
bool CurrentTime::isLeapYear() {
  int year = this->year;
  return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}

void CurrentTime::staticRenewTime(CurrentTime *instance) {
  instance->renewTime();
}

// 更新时间
void CurrentTime::renewTime() {
  this->second += 1;
  if (this->second >= 60) {
    this->second = 0;
    this->minute += 1;
  }
  if (this->minute >= 60) {
    this->minute = 0;
    this->hour += 1;
  }
  if (this->hour >= 24) {
    this->hour = 0;
    this->day += 1;
  }

  int *month_day;
  if (this->isLeapYear()) {
    month_day = this->month_days[1];
  } else {
    month_day = this->month_days[0];
  }

  if (this->day > month_day[this->month - 1]) {
    this->day = 1;
    this->month += 1;
  }
  if (this->month > 12) {
    this->month = 1;
    this->year += 1;
  }
  // this->printQuickTime();
}

// 打印时间
void CurrentTime::printTime() {
  Serial.print("Year:");
  int year = this->year;
  Serial.println(year);

  Serial.print("Month:");
  int month = this->month;
  Serial.println(month);

  Serial.print("Month day:");
  int day = this->day;
  Serial.println(day);

  Serial.print("Hour:");
  int hour = this->hour;
  Serial.println(hour);

  Serial.print("Minute:");
  int minute = this->minute;
  Serial.println(minute);

  Serial.print("Second:");
  int second = this->second;
  Serial.println(second);

  Serial.print("Week day:");
  int weekday = this->weekday;
  Serial.println(weekday);
}

void CurrentTime::printQuickTime() {
  int year = this->year;
  Serial.print(year);

  Serial.print("-");
  int month = this->month;
  if (month < 10) {
    Serial.print("0");
  }
  Serial.print(month);

  Serial.print("-");
  int day = this->day;
  if (day < 10) {
    Serial.print("0");
  }
  Serial.print(day);

  Serial.print(" ");
  int hour = this->hour;
  if (hour < 10) {
    Serial.print("0");
  }
  Serial.print(hour);

  Serial.print(":");
  int minute = this->minute;
  if (minute < 10) {
    Serial.print("0");
  }
  Serial.print(minute);

  Serial.print(":");
  int second = this->second;
  if (second < 10) {
    Serial.print("0");
  }
  Serial.print(second);

  Serial.print(" ");
  int wd = this->weekday;
  String weekday = this->chinese_weekday[wd];
  Serial.println(weekday);
}

int CurrentTime::get_hour() {
  return this->hour;
}

int CurrentTime::get_minute() {
  return this->minute;
}

int CurrentTime::get_second() {
  return this->second;
}

int CurrentTime::get_year() {
  return this->year;
}

int CurrentTime::get_month() {
  return this->month;
}

int CurrentTime::get_day() {
  return this->day;
}

int CurrentTime::get_weekday() {
  return this->weekday;
}

String CurrentTime::get_hour_str() {
  return num2str(this->hour);
}

String CurrentTime::get_minute_str() {
  return num2str(this->minute);
}

String CurrentTime::get_second_str() {
  return num2str(this->second);
}

String CurrentTime::get_calender_str() {
  String result = "";

  result += num2str(this->year);
  result += "-";
  result += num2str(this->month);
  result += "-";
  result += num2str(this->day);
  result += " ";
  result += this->chinese_weekday[this->weekday];

  return result;
}

// 打印时间的格式化函数
String num2str(int digits) {
  String s = "";
  if (digits < 10)
    s = s + "0";
  s = s + digits;
  return s;
}